using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 睡眠Buff - 使目标进入睡眠状态，受到伤害会醒���
/// </summary>
public class SleepBuff : BuffBase
{
    private bool m_OriginalCanMove;
    private bool m_OriginalCanAttack;
    private bool m_OriginalCanCast;
    private bool m_IsAwakened = false;

    public SleepBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
    }

    protected override void OnStart()
    {
        base.OnStart();
        
        // 保存原始状���
        m_OriginalCanMove = Target.MoveComponent != null;
        m_OriginalCanAttack = Target.AttackComponent != null;
        m_OriginalCanCast = Target.SkillAttackIndicators != null;
        
        // 禁用移动、攻击和技���
        if (Target.MoveComponent != null)
        {
            Target.MoveComponent.enabled = false;
        }
        
        if (Target.AttackComponent != null)
        {
            Target.AttackComponent.enabled = false;
        }
        
        if (Target.SkillAttackIndicators != null)
        {
            Target.SkillAttackIndicators.enabled = false;
        }
        
        // 播放睡眠特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        // 设置动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("sleeping", true);
        }
        
        Logger.Log($"{Target.name} 进入睡眠状���");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        
        // 检查是否被伤害唤醒
        if (!m_IsAwakened && Target.HealthComponent.CurHealth < Target.HealthComponent.MaxHealth)
        {
            Awaken();
        }
    }

    private void Awaken()
    {
        m_IsAwakened = true;
        
        // 立即移除睡眠效果
        if (Target.BuffComponent != null)
        {
            Target.BuffComponent.RemoveBuff(BuffType.Sleep);
        }
        
        Logger.Log($"{Target.name} 被伤害唤���");
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        Logger.Log($"{Target.name} 睡眠效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        // 恢复原始状���
        if (m_OriginalCanMove && Target.MoveComponent != null)
        {
            Target.MoveComponent.enabled = true;
        }
        
        if (m_OriginalCanAttack && Target.AttackComponent != null)
        {
            Target.AttackComponent.enabled = true;
        }
        
        if (m_OriginalCanCast && Target.SkillAttackIndicators != null)
        {
            Target.SkillAttackIndicators.enabled = true;
        }
        
        // 恢复动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("sleeping", false);
        }
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("Sleep"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return false; // 睡眠效果不能叠加，只能刷新持续时���
    }
}
